package com.doubi.controller;

import com.doubi.entity.Announce;
import com.doubi.service.AnnounceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.List;

@RestController
@CrossOrigin("*")
public class AnnounceControl {
    @Autowired
    private AnnounceService announceService;

    /**
     * 查询公告类型通知
     * @return
     */
    @GetMapping("/getAnnounce")
    public List<Announce> getAnnounce() {
        List<Announce> list = announceService.list();
        list.sort(Comparator.comparing(Announce::getAnnounceTime).reversed());
        return list;
    }

    @PostMapping("/releaseAnnounce")
    public String addAnnounce(@RequestBody Announce announce) {
        LocalDateTime now=LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowTime=now.format(formatter);
        announce.setAnnounceTime(nowTime);
        boolean save = announceService.save(announce);
        if (save){
            return "success";
        }else {
            return "fail";
        }
    }}
